package com.wenwu.common.tag;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.wenwu.common.bean.RoleResources;
import com.wenwu.common.bean.User;

/**
 * @project base
 * @class PrivilegeButtonTag.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2014年11月25日 下午2:40:52
 * @description 按钮权限检查标签
 */
public class PrivilegeButtonTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String btnname;

	public void setBtnname(String btnname) {
		this.btnname = btnname;
	}

	public int doStartTag() throws JspException {
		HttpSession session = pageContext.getSession();
		// 获取用户登录信息
		User user = (User) session.getAttribute(SysConstants.LOGIN_KEY);
		// 超级用户
		if (user != null && SysConstants.SUPERMAN.equals(user.getUserName())) {
			return SKIP_PAGE;
		}

		// 获取登录后存储的角色资源信息
		@SuppressWarnings("unchecked")
		Map<String, List<RoleResources>> roleResMap = (Map<String, List<RoleResources>>) session
				.getAttribute(SysConstants.ROLE_RESOURCE_KEY);
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		// 请求资源ID值
		String reqResourceID = request.getParameter("sourceid");

		if (roleResMap!=null&&roleResMap.containsKey(reqResourceID)) {
			List<RoleResources> roleResList = roleResMap.get(reqResourceID);
			if (roleResList.size() == 0) {
				return SKIP_BODY;
			}
			for (int a = 0; a < roleResList.size(); a++) {
				RoleResources roleResource = (RoleResources) roleResList.get(a);
				String[] funs = roleResource.getCodes().split(",");
				synchronized (this) {
					for (int i = 0; i < funs.length; i++) {
						if (btnname.equals(funs[i])) {
							return SKIP_PAGE;
						}
					}
				}
			}
		}
		return SKIP_BODY;
	}
}
